#include "includes.h"
#include "MIIC2.h"
/* 函数定义 */
void MIIC2_Init(void);                                         /* 模拟IIC初始化 */
u8 MIIC2_Start(void);                                       /* 模拟IIC启动 */
void MIIC2_Stop(void);                                         /* 模拟IIC停止 */
u8 MIIC2_Write_Byte(u8 iSendByte);                       /* 模拟IIC发送一个字节 */
u8 MIIC2_Read_Byte(void);                                   /* 模拟IIC接收一个字节 */
void MIIC2_delay(void);                                        /* 模拟IIC延时 */
void MIIC2_Ack(void);                                          /* 模拟IIC应答 */
void MIIC2_NoAck(void);                                        /* 模拟IIC不应答 */
u8 MIIC2_WaitAck(void);                                     /* 模拟IIC等待应答 */



/* 模拟IIC初始化 */
void MIIC2_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;  
  
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //原来的初始化代码没有打开时钟 

	/* Configure I2C1 pins: SCL2 and SDA2 */ 
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_I2C_SCK2; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;      // 开漏   
	GPIO_Init(GPIOC, &GPIO_InitStructure); 

	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_I2C_SDA2; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;      // 开漏   
	GPIO_Init(GPIOC, &GPIO_InitStructure); 	

	GPIO_SetBits(GPIOC, GPIO_Pin_I2C_SCK2 | GPIO_Pin_I2C_SDA2);         //这是我加的,初始化后SCL2和SDA2都是高电平 
}
/* 模拟IIC延时 */
void MIIC2_delay(void) 
{ 
/*   u16 i = 100; //这里可以优化速度 ，经测试最低到5还能写入 
   while(i)  
   {  
     i--;  
   } */
 // Delay_nus(10);
	delay_us(10);
} 
/* 模拟IIC启动 */
u8 MIIC2_Start(void)
{
  SDA2_H; 
  SCL2_H; 
  MIIC2_delay(); 
  if(!SDA2_read)return FALSE; //SDA2线为低电平则总线忙,退出 
  SDA2_L; 
  MIIC2_delay(); 
  if(SDA2_read) return FALSE; //SDA2线为高电平则总线出错,退出 
  SDA2_L; 
  MIIC2_delay(); 
  return TRUE; 
}
/* 模拟IIC停止 */
void MIIC2_Stop(void)
{
  SCL2_L; 
  MIIC2_delay(); 
  SDA2_L; 
  MIIC2_delay(); 
  SCL2_H; 
  MIIC2_delay(); 
  SDA2_H; 
  MIIC2_delay(); 
}
/* 模拟IIC应答 */
void MIIC2_Ack(void) 
{ 
  SCL2_L; 
  MIIC2_delay(); 
  SDA2_L; 
  MIIC2_delay(); 
  SCL2_H; 
  MIIC2_delay(); 
  SCL2_L; 
  MIIC2_delay(); 
} 
/* 模拟IIC不应答 */
void MIIC2_NoAck(void) 
{ 
  SCL2_L; 
  MIIC2_delay(); 
  SDA2_H; 
  MIIC2_delay(); 
  SCL2_H; 
  MIIC2_delay(); 
  SCL2_L; 
  MIIC2_delay(); 
} 
/* 模拟IIC等待应答 */
//返回为:=1有ACK,=0无ACK 
u8 MIIC2_WaitAck(void)   
{ 
  GPIO_InitTypeDef  GPIO_InitStructure;
  
  SCL2_L; 
  MIIC2_delay(); 
  SDA2_H; 
  MIIC2_delay(); 
  SCL2_H; 
  /* Configure I2C1 pins: SDA2 */ 
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_I2C_SDA2; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      // 浮空输入 
  GPIO_Init(GPIOC, &GPIO_InitStructure); 
  MIIC2_delay(); 

  if(SDA2_read) 
  { 
    SCL2_L; 
    /* Configure I2C1 pins: SCL2 and SDA2 */ 
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_I2C_SDA2; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;      // 开漏   
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    return FALSE; 
  } 
  SCL2_L; 
  /* Configure I2C1 pins: SCL2 and SDA2 */ 
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_I2C_SDA2; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;      // 开漏   
  GPIO_Init(GPIOC, &GPIO_InitStructure); 
  return TRUE; 
}
/* 模拟IIC发送一个字节 */
u8 MIIC2_Write_Byte(u8 iSendByte)					
{	
  u8 i=8; 
  
  while(i--) 
  { 
    SCL2_L; 
    MIIC2_delay(); 
    if(iSendByte & 0x80){ 
        SDA2_H; 
    }else{  
        SDA2_L; 
    }
    iSendByte<<=1; 
    MIIC2_delay(); 
    SCL2_H; 
    MIIC2_delay(); 
  } 
  SCL2_L; 
/*  
  if(MIIC2_WaitAck() == FALSE){
    return FALSE;
  } */

  return TRUE;
}
/* 模拟IIC接收一个字节 */
u8 MIIC2_Read_Byte(void)
{
  u8 i = 8; 
  u8 ReceiveByte = 0; 
  GPIO_InitTypeDef  GPIO_InitStructure;
  
  SDA2_H; 
  MIIC2_delay(); 
  /* Configure I2C1 pins: SDA2 */ 
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_I2C_SDA2; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      // 浮空输入 
  GPIO_Init(GPIOC, &GPIO_InitStructure); 
  while(i--) 
  { 
    ReceiveByte <<= 1;       
    SCL2_L; 
    MIIC2_delay(); 
    SCL2_H; 
    MIIC2_delay(); 
    if(SDA2_read) 
    { 
      ReceiveByte |= 0x01; 
    } 
  } 
  SCL2_L; 
  /* Configure I2C1 pins: SCL2 and SDA2 */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_I2C_SDA2; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;      // 开漏   
  GPIO_Init(GPIOC, &GPIO_InitStructure); 
  return ReceiveByte; 
}



